package com.example.ogtest.test.class12.t3;

import java.nio.IntBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import com.example.ogtest.test.tool.BufferTool;

import android.R.layout;
import android.opengl.GLSurfaceView.Renderer;

public class MyGlRenderer3 implements Renderer {

	
	float rotateTri,rotateQuad;
	int one = 0x10000;
	
	private int[] triggerBuffer = new int[]{
			0,one,0,
			-one,-one,0,
			one,-one,0,
	};
	private int[] quaterBuffer = new int[]{
			one,one,0,
			-one,one,0,
			one,-one,0,
			-one,-one,0,
	};
	private int[] colorBuffer = new int[]{
		one,0,0,one,
		0,one,0,one,
		0,0,one,one,
	};
	
	
	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		// TODO Auto-generated method stub
		gl.glShadeModel(GL10.GL_SMOOTH);
		gl.glClearColor(0, 0, 0, 0);
		gl.glClearDepthf(1.0f);
		gl.glEnable(GL10.GL_DEPTH_TEST);
		gl.glDepthFunc(GL10.GL_LEQUAL);
		gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
	}

	@Override
	public void onSurfaceChanged(GL10 gl, int width, int height) {
		// TODO Auto-generated method stub
		float ratio = (float)width/height;
		gl.glViewport(0, 0, width, height);
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glLoadIdentity();
		gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glLoadIdentity();
	}

	@Override
	public void onDrawFrame(GL10 gl) {
		// TODO Auto-generated method stub
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		gl.glLoadIdentity();
		
		gl.glTranslatef(-1.5f, 0.0f, -6.0f);
		gl.glRotatef(rotateTri, 0.0f, 1.0f, 0.0f);
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
		gl.glColorPointer(4, GL10.GL_FIXED, 0, BufferTool.bufferUtil(colorBuffer));
		gl.glVertexPointer(3, GL10.GL_FIXED, 0, BufferTool.bufferUtil(triggerBuffer));
		gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
		gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
		gl.glFinish();
		
		
		gl.glLoadIdentity();
		gl.glTranslatef(1.5f, 0.0f, -6.0f);
		gl.glColor4f(0.5f, 0.5f, 1.0f, 1.0f);
		gl.glRotatef(rotateQuad, 1.0f, 0.0f, 0.0f);
		gl.glVertexPointer(3, GL10.GL_FIXED, 0, BufferTool.bufferUtil(quaterBuffer));
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
		gl.glFinish();
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		rotateTri += 0.5f;
		rotateQuad -= 0.5f;
	}

}
